package cn.stylefeng.guns.alioss.controller;


import cn.stylefeng.guns.alioss.model.result.AliossResult;
import cn.stylefeng.guns.alioss.service.AliOssService;
import cn.stylefeng.guns.core.pojo.response.ErrorResponseData;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author Aria
 */
@Api(tags = "图片存储")
@RestController
public class AliOssController {

    @Resource
    private AliOssService aliOssService;

    @ApiOperation("上传文件")
    @ApiModelProperty(value = "上传文件")
    @PostMapping("aliOss/uploadImgFile")
    public String upload(MultipartFile file) {
        String url = aliOssService.upload(file);
        AliossResult aliossResult = new AliossResult();
        aliossResult.setUrl(url);
        return url;
        //return Result.ok().data("url",upload);
    }

    @ApiOperation("删除文件")
    @ApiModelProperty(value = "删除文件")
    @PostMapping("aliOss/deleteImgFile")
    public ResponseData delete(String fileUrl) {
        if (fileUrl == null) {
            return ErrorResponseData.error("图片不存在");
        }
        try {
            fileUrl = fileUrl.split(".com.")[1];
            aliOssService.deleteFile(fileUrl);
        } catch (Exception e) {
        }
        return new SuccessResponseData();
    }
}
